import 'package:environmental_les_app/common/style/colours.dart';
import 'package:environmental_les_app/common/widgets/button_public_rect.dart';
import 'package:environmental_les_app/common/widgets/common_widget.dart';
import 'package:environmental_les_app/page/task_list/widgets/basic_info_card.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';

import 'index.dart';
import 'widgets/text_bg_container.dart';

class TaskDetailPage extends GetView<TaskDetailController> {
  const TaskDetailPage({super.key});

  // 主视图
  Widget _buildView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const MyPageAppBar(
          title: "任务详情",
          backgroundColor: Colors.transparent,
        ),
        Expanded(
          child: SingleChildScrollView(
            child: Column(
              children: [
                const Padding(
                  padding: EdgeInsets.symmetric(horizontal: 15),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "已办结",
                        style: TextStyle(
                          fontSize: 24,
                          color: Colors.black,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      vGap5,
                      Row(
                        children: [
                          Text(
                            "张小猪  2025-08-22 11:23:19",
                            style: TextStyle(
                              fontSize: 14,
                              color: Colours.textC4C,
                            ),
                          ),
                          hGap8,
                          Text(
                            "创建",
                            style: TextStyle(
                              fontSize: 14,
                              color: Colours.text09C,
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                vGap10,
                _buildTaskInfoCard(),
                _buildClueInfoCard(),
                _buildSupervisionInfoCard(),
                _buildProcessFlowCard(),
              ],
            ),
          ),
        ),
        Container(
          color: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 16),
          child: Row(
            children: [
              Expanded(
                child: ButtonPublicRect(
                  margin: const EdgeInsets.only(right: 10),
                  type: ButtonPublicType.outline,
                  text: "督办",
                  onPressed: () {
                    controller.goToSupervision();
                  },
                ),
              ),
              Visibility(
                visible: false,
                child: Expanded(
                  child: ButtonPublicRect(
                    margin: const EdgeInsets.only(right: 10),
                    text: "办结",
                    backgroundColor: Colours.colorFF7A22,
                    textColor: Colors.white,
                    onPressed: () {
                      controller.goToComplete();
                    },
                  ),
                ),
              ),
              Expanded(
                child: ButtonPublicRect(
                  margin: const EdgeInsets.only(right: 10),
                  text: "指派",
                  backgroundColor: Colours.appMain,
                  textColor: Colors.white,
                  onPressed: () {
                    controller.goToDistribute();
                  },
                ),
              ),
              Visibility(
                visible: false,
                child: Expanded(
                  child: ButtonPublicRect(
                    margin: const EdgeInsets.only(right: 10),
                    text: "签收",
                    backgroundColor: Colours.appMain,
                    textColor: Colors.white,
                    onPressed: () {
                      controller.goToSign();
                    },
                  ),
                ),
              ),
              Visibility(
                visible: true,
                child: Expanded(
                  child: ButtonPublicRect(
                    text: "待我处理",
                    backgroundColor: Colours.appMain,
                    textColor: Colors.white,
                    onPressed: () {
                      controller.goToToMeHandle();
                    },
                  ),
                ),
              ),
            ],
          ),
        )
      ],
    );
  }

  Widget _buildTaskInfoCard() {
    bool isExpanded = controller.expansionState['taskInfo'] ?? true;
    return BasicInfoCard(
      title: "任务信息",
      cardKey: 'taskInfo',
      isExpanded: isExpanded,
      onTap: () => controller.toggleCard('taskInfo'),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                decoration: BoxDecoration(
                  color: Colours.bgF4F7FE,
                  borderRadius: BorderRadius.circular(4),
                  border: Border.all(color: Colours.lineB7C0D7),
                ),
                child: const Text(
                  "无人机监测",
                  style: TextStyle(
                      color: Colours.appMain,
                      fontSize: 13,
                      fontWeight: FontWeight.w500),
                ),
              ),
              hGap8,
              const Text(
                "RW453564333",
                style: TextStyle(
                    color: Colours.textC4C,
                    fontSize: 15,
                    fontWeight: FontWeight.w500),
              )
            ],
          ),
          vGap15,
          buildAddressInfoRow("任务地点:", "准格尔旗薛家湾镇环保局准格尔旗薛家湾镇环保局"),
          vGap10,
          TextBgContainer(
            child: Column(
              children: [
                buildInfoRow("任务开始时间:", "2025-08-22 11:29:55"),
                vGap5,
                buildInfoRow("任务截止时间:", "2025-08-22 11:29:55"),
              ],
            ),
          ),
          vGap10,
          TextBgContainer(
            child: Column(
              children: [
                buildInfoRow("任务接收单位:", "准格尔旗薛家湾镇环保局"),
                vGap5,
                buildInfoRow("任务处理时间:", "2025-08-22 11:29:55"),
              ],
            ),
          ),
          vGap10,
          TextBgContainer(
              child: buildExplanationInfoRow(
                  "任务说明", "这是具体的描述信息这是具体的描述信息这是具体的描述信息")),
          vGap10,
          TextBgContainer(
              child:
                  buildAddressInfoRow("备注:", "请在周边100米范围内进行巡备注备注备注备注备注备注备注查")),
        ],
      ),
    );
  }

  Widget _buildClueInfoCard() {
    bool isExpanded = controller.expansionState['clueInfo'] ?? true;
    return BasicInfoCard(
      title: "线索信息",
      cardKey: 'clueInfo',
      onTap: () => controller.toggleCard('clueInfo'),
      isExpanded: isExpanded,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Wrap(
            spacing: 10,
            runSpacing: 10,
            children: [
              ...controller.cluesToShow.map((e) => Container(
                    padding:
                        const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                    decoration: BoxDecoration(
                      color: e == "线索 1" ? Colours.bgECF5FF : Colours.bgF7F8FA,
                      borderRadius: BorderRadius.circular(4),
                      border: Border.all(
                          color: e == "线索 1"
                              ? Colours.line629EFF
                              : Colors.transparent),
                    ),
                    child: Text(
                      e,
                      style: TextStyle(
                          color:
                              e == "线索 1" ? Colours.appMain : Colours.text969,
                          fontSize: 14),
                    ),
                  )),
              if (controller.canShowMoreClues)
                GestureDetector(
                  onTap: () => controller.toggleCluesExpansion(),
                  child: Container(
                    padding:
                        const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                    decoration: BoxDecoration(
                      color: Colours.bgF7F8FA,
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: Text(
                      controller.isCluesExpanded ? "收起" : "更多",
                      style:
                          const TextStyle(color: Colours.text969, fontSize: 14),
                    ),
                  ),
                ),
            ],
          ),
          vGap10,
          Row(
            children: [
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                decoration: BoxDecoration(
                  color: Colours.bgF4F7FE,
                  borderRadius: BorderRadius.circular(4),
                  border: Border.all(color: Colours.lineB7C0D7),
                ),
                child: const Text(
                  "空气站点",
                  style: TextStyle(
                      color: Colours.appMain,
                      fontSize: 13,
                      fontWeight: FontWeight.w500),
                ),
              ),
              hGap8,
              const Text(
                "环保局",
                style: TextStyle(
                    color: Colours.textC4C,
                    fontSize: 17,
                    fontWeight: FontWeight.w500),
              )
            ],
          ),
          vGap8,
          buildAddressInfoRow("报警地点:", "准格尔旗薛家湾镇环保局"),
          vGap10,
          TextBgContainer(child: buildInfoRow("报警时间:", "2025-08-22 11:29:55")),
          vGap10,
          TextBgContainer(
            child: buildExplanationInfoRow(
                "这是报警类型", "无人机监测PM2.5监测值200,超标2倍,发生报警(报警详情)"),
          ),
        ],
      ),
    );
  }

  Widget _buildSupervisionInfoCard() {
    bool isExpanded = controller.expansionState['supervisionInfo'] ?? true;
    return BasicInfoCard(
      title: "督办信息",
      cardKey: 'supervisionInfo',
      onTap: () => controller.toggleCard('supervisionInfo'),
      isExpanded: isExpanded,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                decoration: BoxDecoration(
                  color: Colours.bgF4F7FE,
                  borderRadius: BorderRadius.circular(4),
                  border: Border.all(color: Colours.lineB7C0D7),
                ),
                child: const Text(
                  "执法大队",
                  style: TextStyle(
                      color: Colours.appMain,
                      fontSize: 12,
                      fontWeight: FontWeight.w500),
                ),
              ),
              hGap8,
              const Text(
                "猩猩怪 (督办人)",
                style: TextStyle(
                    color: Colours.textC4C,
                    fontSize: 15,
                    fontWeight: FontWeight.w500),
              )
            ],
          ),
          vGap10,
          buildAddressInfoRow("督办时间:", "2025-08-21 14:44"),
          vGap10,
          TextBgContainer(
              child: buildExplanationInfoRow(
                  "督办内容", "这是具体的描述信息这是具体的描述信息这是具体的描述信息")),
        ],
      ),
    );
  }

  Widget _buildProcessFlowCard() {
    bool isExpanded = controller.expansionState['processFlow'] ?? true;
    return BasicInfoCard(
      title: "处理流程",
      cardKey: 'processFlow',
      onTap: () => controller.toggleCard('processFlow'),
      isExpanded: isExpanded,
      child: Column(
        children: [
          buildProcessStep("05", "创建任务", "2024/11/11 11:11:11", "值守中心", "王二",
              "任务说明这是说明说明这是说明说明这是说明说明", "执法大队部门", true),
          buildProcessStep("04", "签收", "2024/11/11 11:11:11", "值守指挥中心", "王二",
              null, "执法大队部门", true),
          buildProcessStep("03", "下派", "2024/11/11 11:11:11", "值守中心", "王二",
              null, "执法大队部门", true),
          buildProcessStep("02", "处理填报", "2024/11/11 11:11:11", "值守中心", "王二",
              null, "执法大队部门", true,
              detail: _buildReportDetail()),
          buildProcessStep("01", "驳回", "2024/11/11 11:11:11", "值守中心", "王二",
              null, "执法大队部门", false),
        ],
      ),
    );
  }

  Widget _buildReportDetail() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        vGap10,
        const Divider(),
        vGap10,
        const Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              "详情",
              style: TextStyle(
                  color: Colours.text969,
                  fontSize: 14,
                  fontWeight: FontWeight.w500),
            ),
            Row(
              children: [
                Text(
                  "收起",
                  style: TextStyle(fontSize: 14, color: Colours.appMain),
                ),
                Icon(
                  Icons.arrow_drop_up_outlined,
                  color: Colours.appMain,
                )
              ],
            )
          ],
        ),
        vGap10,
        buildAddressInfoRow("是否有问题:", "是"),
        buildAddressInfoRow("排查情况:", "到底是啥问题就是啥问题"),
        buildAddressInfoRow("处理措施:", "先这样那一趟再这一趟最后再去一趟"),
        Row(
          children: [
            const Text(
              "影像信息:",
              style: TextStyle(color: Colours.text09C, fontSize: 14),
            ),
            hGap8,
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
              decoration: BoxDecoration(
                color: Colours.appMain,
                borderRadius: BorderRadius.circular(4),
              ),
              child: const Text(
                "查看(3)",
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 14,
                    fontWeight: FontWeight.w500),
              ),
            )
          ],
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<TaskDetailController>(
      init: TaskDetailController(),
      id: "task_detail",
      builder: (_) {
        return Scaffold(
          body: Stack(
            children: [
              Container(
                height: 200,
                decoration: BoxDecoration(
                    image: DecorationImage(
                  image: AssetImage(ImageUtils.getImgPath("bg_title_top")),
                  fit: BoxFit.cover,
                )),
              ),
              _buildView(),
            ],
          ),
        );
      },
    );
  }
}
